/*
 * BusinessDialog.java
 * business
 *
 * Created by ChenTao on 2017/1/18.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;

/**
 * BusinessDialog 通用对话框
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/8 16:25
 */
public class BusinessDialog extends Dialog {

    private BusinessDialogController mController;

    private TextView mTvTitle;

    private TextView mTvContent;

    private TextView mTvPositive;

    private TextView mTvNegative;

    private LinearLayout mButtonsLayout;

    public BusinessDialog(Context context) {
        super(context, R.style.ThemeNoTitleDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_business);

        initViews();
    }

    private void initViews() {
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mTvPositive = (TextView) findViewById(R.id.tv_confirm);
        mTvNegative = (TextView) findViewById(R.id.tv_cancel);
        mButtonsLayout = (LinearLayout) findViewById(R.id.buttons_layout);
    }

    private void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            mTvTitle.setVisibility(View.VISIBLE);
            mTvTitle.setText(title);
        }
    }

    private void setMessage(String content) {
        if (!TextUtils.isEmpty(content)) {
            mTvContent.setVisibility(View.VISIBLE);
            mTvContent.setText(content);
        }
    }

    private void setCancelText(String cancelText, final DialogInterface.OnClickListener listener) {
        if (!TextUtils.isEmpty(cancelText)) {
            mTvNegative.setText(cancelText);
        }
        mTvNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(BusinessDialog.this, DialogInterface.BUTTON_NEGATIVE);
                }
            }
        });
    }

    private void setConfirmText(
            String confirmText, final DialogInterface.OnClickListener listener) {
        if (!TextUtils.isEmpty(confirmText)) {
            mTvPositive.setText(confirmText);
        }
        mTvPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(BusinessDialog.this, DialogInterface.BUTTON_POSITIVE);
                }
            }
        });
    }

    @Override
    public void show() {
        super.show();

        if (mController == null) {
            throw new NullPointerException("You must use BusinessDialog.Builder to create BusinessDialog to show !");
        }

        setTitle(mController.getTitle());
        setMessage(mController.getMessage());
        setContentGravity(mController.getContentGravity());
        setConfirmText(mController.getPositiveText(), mController.getPositiveListener());
        if (mController.isSingleConfirmButton()) {
            setSingleConfirmButton(mController.getSingleConfirmListener());
        } else {
            setCancelText(mController.getNegativeText(), mController.getNegativeListener());
        }
        setCancelable(mController.isCancelable());
    }

    private void setSingleConfirmButton(final OnClickListener listener) {
        mTvNegative.setVisibility(View.GONE);

        // reset Confirm Button Layout
        LinearLayout.LayoutParams params =
                (LinearLayout.LayoutParams) mTvPositive.getLayoutParams();
        params.leftMargin = 0;
        mTvPositive.setLayoutParams(params);
        mButtonsLayout.setGravity(Gravity.CENTER);

        mTvPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (listener != null) {
                    listener.onClick(BusinessDialog.this, DialogInterface.BUTTON_POSITIVE);
                }
            }
        });
    }

    private void setContentGravity(int gravity) {
        if (gravity != Gravity.NO_GRAVITY) {
            LinearLayout.LayoutParams params =
                    (LinearLayout.LayoutParams) mTvContent.getLayoutParams();
            params.gravity = gravity;

            mTvContent.setLayoutParams(params);
            mTvContent.setGravity(gravity);
        }
    }

    private void setController(BusinessDialogController controller) {
        mController = controller;
    }

    public static class Builder {

        private BusinessDialogController mController;

        public Builder(Context context) {
            mController = new BusinessDialogController(context);
        }

        public Builder setTitle(String title) {
            mController.setTitle(title);
            return this;
        }

        public Builder setTitle(@StringRes int titleResId) {
            return setTitle(mController.getContext().getResources().getString(titleResId));
        }

        public Builder setMessage(String content) {
            mController.setMessage(content);
            return this;
        }

        public Builder setMessage(@StringRes int contentResId) {
            return setMessage(mController.getContext().getResources().getString(contentResId));
        }

        public Builder setContentGravity(int gravity) {
            mController.setContentGravity(gravity);
            return this;
        }

        public Builder setNegativeButton(String text, final OnClickListener listener) {
            mController.setNegativeButtonText(text);
            mController.setNegativeButtonListener(listener);
            return this;
        }

        public Builder setNegativeButton(@StringRes int textId, final OnClickListener listener) {
            return setNegativeButton(mController.getContext().getString(textId), listener);
        }

        public Builder setPositiveButton(String text, final OnClickListener listener) {
            mController.setPositiveButtonText(text);
            mController.setPositiveButtonListener(listener);
            return this;
        }

        public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
            return setPositiveButton(mController.getContext().getString(textId), listener);
        }

        public Builder setSinglePositiveButton(String text, final OnClickListener listener) {
            mController.setPositiveButtonText(text);
            mController.setSingleConfirmButton(true);
            mController.setSingleConfirmListener(listener);
            return this;
        }

        public Builder setSinglePositiveButton(@StringRes int textId, final OnClickListener listener) {
            return setSinglePositiveButton(mController.getContext().getString(textId), listener);
        }

        public Builder setCancelable(boolean cancelable) {
            mController.setCancelable(cancelable);
            return this;
        }

        public BusinessDialog create() {
            BusinessDialog dialog = new BusinessDialog(mController.getContext());
            dialog.setController(mController);
            return dialog;
        }
    }
}
